package com.martin.fastframe.ui.page.index

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log
import android.view.View
import com.martin.fastframe.R
import com.martin.lib_base.ktx.loge
import dev.utils.app.SizeUtils

class PageIndexView : View {

    private val _paint by lazy {
        Paint()
            .apply {
                color = Color.WHITE
                style = Paint.Style.FILL
                strokeWidth = 5f
            }
    }

    //点大小
    private val _pointSize = SizeUtils.dp2px(4f).toFloat()

    private val _selectWidth = SizeUtils.dp2px(12f).toFloat()

    //间隔
    private val _interval = SizeUtils.dp2px(2f).toFloat()

    //页面数量
    var pageCount = 1
        set(value) {
            field = value
            requestLayout()
        }

    //当前页码
    var index = 0
        set(value) {
            field = value
            invalidate()
        }

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int
    ) : super(context, attrs, defStyleAttr, defStyleRes) {
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val w = ((_pointSize + _interval) * (pageCount - 1)) + _selectWidth
        setMeasuredDimension(w.toInt(), _pointSize.toInt())
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val radius = _pointSize / 2
        var xS = 0f
        for (i in 0 until pageCount) {
            loge("xS=$xS ; _pointSize=$_pointSize ; _selectWidth=$_selectWidth ; _interval=$_interval")
            if (index == i) {
                _paint.color = context.resources.getColor(dev.utils.R.color.black_tran40)
                canvas.drawRoundRect(xS, 0f, xS + _selectWidth, _pointSize, radius, radius, _paint)
                xS += _selectWidth + _interval
            } else {
                _paint.color = context.resources.getColor(dev.utils.R.color.black_tran40)
                canvas.drawCircle(xS + radius, radius, radius, _paint)
                xS += _pointSize + _interval
            }
        }

    }
}